using System;
using System.IO;
using System.Text;
namespace Common {
    public class FileHelper : IDisposable {
        private bool _alreadyDispose;

        public static void CopyDir(string srcPath, string aimPath) {
            try {
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar) {
                    aimPath = aimPath + Path.DirectorySeparatorChar;
                }
                if (!Directory.Exists(aimPath)) {
                    Directory.CreateDirectory(aimPath);
                }
                foreach (string str in Directory.GetFileSystemEntries(srcPath)) {
                    if (Directory.Exists(str)) {
                        CopyDir(str, aimPath + Path.GetFileName(str));
                    }
                    else {
                        File.Copy(str, aimPath + Path.GetFileName(str), true);
                    }
                }
            }
            catch (Exception exception) {
                throw new Exception(exception.ToString());
            }
        }

        public static void DeleteFolder(string dir) {
            if (Directory.Exists(dir)) {
                foreach (string str in Directory.GetFileSystemEntries(dir)) {
                    if (File.Exists(str)) {
                        File.Delete(str);
                    }
                    else {
                        DeleteFolder(str);
                    }
                }
                Directory.Delete(dir);
            }
        }

        public void Dispose() {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool isDisposing) {
            if (!this._alreadyDispose) {
                this._alreadyDispose = true;
            }
        }

        public static void FileAdd(string Path, string strings) {
            StreamWriter writer = File.AppendText(Path);
            writer.Write(strings);
            writer.Flush();
            writer.Close();
        }

        public static void FileCoppy(string orignFile, string NewFile) {
            File.Copy(orignFile, NewFile, true);
        }

        public static void FileDel(string Path) {
            File.Delete(Path);
        }

        public static void FileMove(string orignFile, string NewFile) {
            File.Move(orignFile, NewFile);
        }

        ~FileHelper() {
            this.Dispose();
        }

        public static void FolderCreate(string orignFolder, string NewFloder) {
            Directory.SetCurrentDirectory(orignFolder);
            Directory.CreateDirectory(NewFloder);
        }

        public static string GetPostfixStr(string filename) {
            int startIndex = filename.LastIndexOf(".");
            int length = filename.Length;
            return filename.Substring(startIndex, length - startIndex);
        }

        public static string ReadFile(string Path, Encoding encode) {
            string str = "";
            if (!File.Exists(Path)) {
                return "不存在相应的目录";
            }
            StreamReader reader = new StreamReader(Path, encode);
            str = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            return str;
        }

        public static void WriteFile(string Path, string Strings, Encoding encode) {
            if (!File.Exists(Path)) {
                File.Create(Path).Close();
            }
            StreamWriter writer = new StreamWriter(Path, false, encode);
            writer.Write(Strings);
            writer.Close();
            writer.Dispose();
        }
    }
}

